//------------------------------------------------------------------------------
// Fichiers d'entée utilisé
//------------------------------------------------------------------------------
#include "MaskTools.hpp"
#include <iostream>
#include <fstream>

//------------------------------------------------------------------------------
// Definition de constantes
//------------------------------------------------------------------------------

namespace ImageAnalysis
{

        //----------------------------------------------------------------------
        // Destructeur
        //----------------------------------------------------------------------
    	MaskTools::~MaskTools()
    	{

        }

	//----------------------------------------------------------------------
        //
        //----------------------------------------------------------------------
        Mask** MaskTools::BuildRobertsMasks()
        {
		Mask** robertMasks = new Mask*[2];

            	robertMasks[1] = new Mask(3, Orientation::DIRECTION_0 );
		robertMasks[1]->Value[0] = 0;
		robertMasks[1]->Value[1] = 0;
		robertMasks[1]->Value[2] = 0;
		
		robertMasks[1]->Value[3] = 0;
		robertMasks[1]->Value[4] = 1;
		robertMasks[1]->Value[5] = 0;
		
		robertMasks[1]->Value[6] = 0;
		robertMasks[1]->Value[7] = 0;
		robertMasks[1]->Value[8] = -1;
		
		robertMasks[0] = new Mask(3, Orientation::DIRECTION_2 );
		robertMasks[0]->Value[0] =  0;
		robertMasks[0]->Value[1] =  0;
		robertMasks[0]->Value[2] =  0;
		
		robertMasks[0]->Value[3] =  0;
		robertMasks[0]->Value[4] =  1;
		robertMasks[0]->Value[5] =  0;
		
		robertMasks[0]->Value[6] = -1;
		robertMasks[0]->Value[7] =  0;
		robertMasks[0]->Value[8] =  0;
		
		return robertMasks;
        }

        //----------------------------------------------------------------------
        //
        //----------------------------------------------------------------------
        Mask** MaskTools::BuildPrewittMasks()
        {
		Mask** prewittMasks = new Mask*[2];

		prewittMasks[1] = new Mask(3, Orientation::DIRECTION_2 );
		prewittMasks[1]->Value[0] = -1;
		prewittMasks[1]->Value[1] = -1;
		prewittMasks[1]->Value[2] = -1;
		
		prewittMasks[1]->Value[3] = 0;
		prewittMasks[1]->Value[4] = 0;
		prewittMasks[1]->Value[5] = 0;
		
		prewittMasks[1]->Value[6] = 1;
		prewittMasks[1]->Value[7] = 1;
		prewittMasks[1]->Value[8] = 1;
		
		prewittMasks[0] = new Mask(3, Orientation::DIRECTION_0 );
		prewittMasks[0]->Value[0] = -1;
		prewittMasks[0]->Value[1] =  0;
		prewittMasks[0]->Value[2] =  1;
		
		prewittMasks[0]->Value[3] = -1;
		prewittMasks[0]->Value[4] =  0;
		prewittMasks[0]->Value[5] =  1;
		
		prewittMasks[0]->Value[6] = -1;
		prewittMasks[0]->Value[7] =  0;
		prewittMasks[0]->Value[8] =  1;
		
		return prewittMasks;
        }

	Mask** MaskTools::BuildSobelMask()
        {
		Mask** sobelMask = new Mask*[3];
		sobelMask[0] = new Mask(3, Orientation::DIRECTION_0 );
		sobelMask[0]->Value[0] = -1;
		sobelMask[0]->Value[1] =  0;
		sobelMask[0]->Value[2] =  1;
		
		sobelMask[0]->Value[3] = -2;
		sobelMask[0]->Value[4] =  0;
		sobelMask[0]->Value[5] =  2;
		
		sobelMask[0]->Value[6] = -1;
		sobelMask[0]->Value[7] =  0;
		sobelMask[0]->Value[8] =  1;
		
		sobelMask[1] = new Mask(3, Orientation::DIRECTION_2 );
		sobelMask[1]->Value[0] = -1;
		sobelMask[1]->Value[1] = -2;
		sobelMask[1]->Value[2] = -1;
		
		sobelMask[1]->Value[3] =  0;
		sobelMask[1]->Value[4] =  0;
		sobelMask[1]->Value[5] =  0;
		
		sobelMask[1]->Value[6] =  1;
		sobelMask[1]->Value[7] =  2;
		sobelMask[1]->Value[8] =  1;
		
		sobelMask[2] = new Mask(3, Orientation::DIRECTION_1 );
		sobelMask[2]->Value[0] =  0;
		sobelMask[2]->Value[1] =  1;
		sobelMask[2]->Value[2] =  2;
		
		sobelMask[2]->Value[3] = -1;
		sobelMask[2]->Value[4] =  0;
		sobelMask[2]->Value[5] =  1;
		
		sobelMask[2]->Value[6] = -2;
		sobelMask[2]->Value[7] = -1;
		sobelMask[2]->Value[8] =  0;


            	return sobelMask;
        }

	Mask** MaskTools::Build55Mask()
	{
			Mask** mask = new Mask*[1];
			mask[0] = new Mask(5, Orientation::DIRECTION_2 );
			mask[0]->Value[0] = -2;
			mask[0]->Value[1] = -2;
			mask[0]->Value[2] = -2;
			mask[0]->Value[3] = -2;
			mask[0]->Value[4] = -2;
		
			mask[0]->Value[5] = -1;
			mask[0]->Value[6] = -1;
			mask[0]->Value[7] = -1;
			mask[0]->Value[8] = -1;
			mask[0]->Value[9] = -1;

			mask[0]->Value[10] = 0;
			mask[0]->Value[11] = 0;
			mask[0]->Value[12] = 0;
			mask[0]->Value[13] = 0;
			mask[0]->Value[14] = 0;

			mask[0]->Value[15] = 1;
			mask[0]->Value[16] = 1;
			mask[0]->Value[17] = 1;
			mask[0]->Value[18] = 1;
			mask[0]->Value[19] = 1;

			mask[0]->Value[20] = 2;
			mask[0]->Value[21] = 2;
			mask[0]->Value[22] = 2;
			mask[0]->Value[23] = 2;
			mask[0]->Value[24] = 2;

            return mask;
        }

	//----------------------------------------------------------------------
        //
        //----------------------------------------------------------------------
	Mask** MaskTools::LoadMask( std::string& filename, int& numberOfMask )
	{	
		std::ifstream file( filename.c_str() );

		if( file.fail() )
		{	
			std::cout << "Impossible d'ouvrir le fichier " << filename << std::endl; 
			return 0;
		}
		
	    	int maskSize;
		int direction;		
		file >> numberOfMask;
		file >> maskSize;						
		Mask** masksResult;		
		masksResult = new Mask*[numberOfMask];		
		
		for(int maskIndex=0; maskIndex< numberOfMask; ++maskIndex )
		{									
			// Création du mask
			file >> direction;
			masksResult[maskIndex] = new Mask( maskSize, Orientation::ConvertDirectionToTDirection(direction) );
			
			// Récupération des valeurs du mask courant
			for( int j=0; j<maskSize; ++j )
			{				
				for( int i=0; i<maskSize; ++i )
				{					
					file >> masksResult[maskIndex]->Value[  i + j*maskSize ];
					std::cout << i << " " << j << " value:" << masksResult[maskIndex]->Value[  i + j*maskSize ] << "\n" << std::flush;
				}
			}
		}		
		return masksResult;
	}

}
